<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mediamgr extends Controller{

  var $_pageName;
  var $_basePageName;
  var $_message = "";
  var $_defaultPage="index";

   function Mediamgr() {
      parent::Controller();
   }

    function index()
    {
        if (!$this->user->isLogged())
            redirect('/wiki');
        $output['isLogged']=$this->user->isLogged();
        $this->load->view('admin/mediamgr/show.html.php',$output);
    }

    function filelist()
    {
        $handle = opendir(MEDIA_DIR);
        $forbid = array(".", "..",".svn", "Thumbs.db", "thumbs","galleries");
        while ($file = readdir($handle)) {
            if(!in_array($file, $forbid)) {
                $thumbFile=$this->_makeThumb(MEDIA_DIR.$file);
                echo '<div class="vignette" style="background-image:url(\''.$thumbFile.'\')" title="'.$file.'" ><span style="display:none" >'.$file.'</span></div>';
            }
        }
    }
    
    //
    function do_upload()
    {
        if (!$this->user->isAuthorized("/admin/upload_media")) {
            die("<script>window.parent.displayMessage('You\'re not allowed to upload media files.');</script>");
        }
    		$config['upload_path'] = MEDIA_DIR;
    		$config['allowed_types'] = $this->obj->config->item("allowed_types");
    		//'gif|jpg|png|swf|mp3|pdf';
    	//	$config['max_size']	= '100';
    	//	$config['max_width']  = '1024';
        //	$config['max_height']  = '768';
    		
    		$this->load->library('upload', $config);
    	
    		if ( ! $this->upload->do_upload()) {
    			echo "<script>window.parent.displayMessage('".$this->upload->display_errors()."');</script>";   	
    		} else {
    			$uploadData = $this->upload->data();
    			//print_r($uploadData);
    			$this->load->helper('normalize');
    			$newfilename=normalizeStr($uploadData['raw_name']).strtolower($uploadData['file_ext']);
    			rename($uploadData['full_path'],$uploadData['file_path'].$newfilename);
                if ($uploadData['is_image']) {
                    //resize the image
                    //reduce only??
                    $config['source_image'] = $uploadData['file_path'].$newfilename;
                    $config['width'] = 800;
                    $config['height']= 600;
                    $this->load->library('image_lib');
                    $this->image_lib->initialize($config);
                    //$this->image_lib->resize();
                }  
                echo "<script>window.parent.updateList();</script>";   			
    		}
    }
    
    function delete($filename)
    {
        if (!$this->user->isAuthorized("/admin/delete_media")) {
            echo "<script>window.parent.displayMessage('You\'re not allowed to delete media files.');</script>";
        } else if (file_exists(MEDIA_DIR. $filename)) {
            unlink(MEDIA_DIR. $filename);
            unlink(MEDIA_DIR. 'thumbs/thumb_' . $filename);
            echo "<script>window.parent.updateList();</script>";
        } else {
            echo "<script>window.parent.displayMessage('File does not exists');</script>";
        }
    }

   /**
    * Makes a thumbnail and returns its url
    * @param  string $file image filename
    * @return string thumbnail filename
    */
    function _makeThumb($file)
    {
        if (file_exists($file)) {
            $fileinfo = pathinfo($file);
            $images_ext = array ("png","gif","jpg");
            if (in_array($fileinfo['extension'], $images_ext)) {
                $thumbfile=MEDIA_DIR.'thumbs/thumb_'.$fileinfo['basename'];
                if (!file_exists($thumbfile)) {
                    //thumb needs to be created
                    $config['source_image'] = $file;
                    $config['width'] = 100;
                    $config['height']= 100;
                    $config['new_image'] = $thumbfile;
                    $this->load->library('image_lib');
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                }
                return BASEURL.$thumbfile;
            } else {
                if(@file_exists(BASEURL.IMG_DIR.$fileinfo['extension'].".gif"))
                    return BASEURL.IMG_DIR.$fileinfo['extension'].".gif";
                else
                    return BASEURL.IMG_DIR."undefined.gif";
            }
        } else {
            return BASEURL.IMG_DIR."doesnotexist.gif";
        }      
    }

}
?>